//
//  ErgeShowVC.m
//  儿歌接口类
//
//  Created by 徐英杰 on 16/4/20.
//  Copyright © 2016年 Jeff. All rights reserved.
//

#import "ErgeShowVC.h"
#define VideoThumbnailWidth  [UIScreen mainScreen].bounds.size.width - 17
#define VideoThumbnailHeight [UIScreen mainScreen].bounds.size.height / 2 - 50
@interface ErgeShowVC ()
@property (weak, nonatomic) IBOutlet UIWebView *showWebView;
@property (nonatomic,copy) NSString *jsString;
@end

@implementation ErgeShowVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *videoURL = self.videoUrl;
    
    NSString *encodedURL=[videoURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *youkuId = [self getYoukuVideoIdByUrl:encodedURL];
    
    
    NSString *js = @"<div id=\"youkuplayer\" style=\"width:%fpx;height:%fpx\"></div> <script type=\"text/javascript\" src=\"http://player.youku.com/jsapi\"> player = new YKU.Player('youkuplayer',{ styleid: '0', client_id: 'b1b8d127374e42ee', vid: '%@',autoplay: true}); </script>";
    
    
    self.jsString = [NSString stringWithFormat:js,VideoThumbnailWidth,VideoThumbnailHeight,youkuId];
    
    [self.showWebView loadHTMLString:self.jsString baseURL:nil]; //webview加载视频
    
}
/** 截取出优酷视频的id*/
-(NSString *)getYoukuVideoIdByUrl:(NSString *)videourl
{
    if (videourl != nil && ![videourl isEqualToString:@""]) {
        NSString *youkuId;
        //        httv.youku.com/v_show/   id_XOTUzODgyOTA0  .html?f=23786645&o=1#paction
        
        NSArray *firSep = [videourl componentsSeparatedByString:@"id_"];
        if ([firSep count] > 0 && firSep != nil)
        {
            NSArray *secSep = [[firSep lastObject] componentsSeparatedByString:@"."];
            if ([secSep count] >0 && secSep != nil)
            {
                youkuId = [secSep firstObject];
            }
        }
        return youkuId;
        
    } else {
        return @"";
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
